package org.gen.pojo.core;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.gen.pojo.config.GenConfig;
import org.gen.pojo.util.DBMetaUtil;
import org.gen.pojo.util.MavenModuleUtil;

import java.io.*;
import java.sql.*;
import java.util.*;

/**
 * Created by zhuge on 2017/10/11.
 */
public class GenerateCore extends Generate{
    //添加模板需要的数据
    @Override
    protected void rebuildConfigRoot(String table_name, List<Attribute> attr_list) {
        //freemarker 的数据容器 添加属性集合
        root.put("attrs", attr_list);
        //判断是否包含日期，包含则添加import java.util.Date; 否则不添加
        if (hasDateType(attr_list)) {
            root.put("hasDateType", new Object());
        } else {
            root.put("hasDateType", null);
        }
        //freemarker 的数据容器 添加表名
        root.put("className", tableNameToClassName(table_name));
        root.put("tableName",table_name);
        root.put("tableComment",getTableComment(table_name));
    }

    @Override
    public void gen() throws Exception{
        //模板目录
        File tempdir = new File(moduleRootPath + "/src/main/resources/templates/");
        Configuration cfg = new Configuration(Configuration.getVersion());
        cfg.setDirectoryForTemplateLoading(tempdir);
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        //调用生成各种代码的方法
        genpojo(cfg);
        genMapper(cfg);
        genMapperXML(cfg);
        genPinyougouInterface(cfg);
        genPinyougouServiceImpl(cfg);
        genPinyougouController(cfg);
        genPinyougouJsController(cfg);
        genPinyougouJsService(cfg);
        genPinyougouHtml(cfg);

    }
    //生成每个对象的管理页面
    private void genPinyougouHtml(Configuration cfg) throws Exception {
        Template template = cfg.getTemplate("pojo.html.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.pojoHtmlPackageName);
        String className = root.get("className").toString();
        String fileName = UncapFirstString(className.substring(2)) + ".html";
        commonGenerateFile(packageDir ,fileName,template);
    }
    //生成品优购angular js控制器的方法
    private void genPinyougouJsController(Configuration cfg) throws Exception {
        Template template = cfg.getTemplate("controller.js.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.jsControllerPackageName);
        String fileName = UncapFirstString(root.get("className").toString()) + "Controller.js";
        commonGenerateFile(packageDir ,fileName,template);
    }

    //生成品优购angular js服务的方法
    private void genPinyougouJsService(Configuration cfg) throws Exception {
        Template template = cfg.getTemplate("service.js.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.jsServicePackageName);
        String fileName = UncapFirstString(root.get("className").toString()) + "Service.js";
        commonGenerateFile(packageDir ,fileName,template);
    }


    //生成品优购控制器的方法
    private void genPinyougouController(Configuration cfg) throws Exception {
        Template template = cfg.getTemplate("controller.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.controllerPackageName);
        String fileName = root.get("className") + "Controller.java";
        commonGenerateFile(packageDir ,fileName,template);
    }
    //生成品优购服务接口的方法
    private void genPinyougouInterface(Configuration cfg) throws Exception {
        Template template = cfg.getTemplate("service.interface.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.serviceInterfacePackageName);
        String fileName = root.get("className") + "Service.java";
        commonGenerateFile(packageDir ,fileName,template);
    }

    //生成品优购服务接口实现类的方法
    private void genPinyougouServiceImpl(Configuration cfg) throws Exception {
        Template template = cfg.getTemplate("service.impl.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.serviceInterfacePackageName+".impl");
        String fileName = root.get("className") + "ServiceImpl.java";
        commonGenerateFile(packageDir ,fileName,template);
    }



    //生成mapper接口的方法
    private void genMapper(Configuration cfg ) throws Exception{
        Template template = cfg.getTemplate("mapper.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.mapperPackageName);
        String fileName = root.get("className") + "Mapper.java";
        commonGenerateFile(packageDir ,fileName,template);

    }
    //生成mapper.xml的方法
    private void genMapperXML(Configuration cfg ) throws Exception{
        Template template = cfg.getTemplate("mapper.xml.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.mapperPackageName);
        String fileName = root.get("className") + "Mapper.xml";
        commonGenerateFile(packageDir ,fileName,template);
    }
    //生成pojo的方法
    private void genpojo(Configuration cfg ) throws Exception{
        Template template = cfg.getTemplate("pojo.java.ftl");  // load E:/Work/Freemarker/templates/person.ftl
        String packageDir = packageToDir(GenConfig.packageName);
        String fileName = root.get("className") + ".java";
        commonGenerateFile(packageDir ,fileName,template);
    }



}
